//
//  UITextField+XWKit.m
//  Tiipvi
//
//  Created by xiaowuxiaowu on 2017/4/26.
//  Copyright © 2017年 xiaowuxiaowu. All rights reserved.
//

#import "UITextField+XWKit.h"
#import "UITextField+Blocks.h"
#import "UIButton+CountDown.h"
@implementation UITextField (XWKit)

+(instancetype)initWithPlaceholder:(NSString *)placeholder text:(NSString *)text{
    return [self initWithFrame:CGRectZero fontSize:16.0 placeholder:placeholder text:text];
}

+(instancetype)initWithFrame:(CGRect)frame  placeholder:(NSString *)placeholder text:(NSString *)text{
      return [self initWithFrame:frame fontSize:16.0 placeholder:placeholder text:text];
}

+(instancetype)initWithFrame:(CGRect)frame fontSize:(CGFloat)fontSize placeholder:(NSString *)placeholder text:(NSString *)text{

    return [self initWithFrame:frame borderStyle:UITextBorderStyleRoundedRect fontSize:fontSize placeholder:placeholder text:text];
}

+(instancetype)initWithFrame:(CGRect)frame borderStyle:(UITextBorderStyle)borderStyle fontSize:(CGFloat)fontSize placeholder:(NSString *)placeholder text:(NSString *)text{

    UITextField *textField = [[UITextField alloc]initWithFrame:frame];
    textField.borderStyle = borderStyle;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    textField.font = fontSystemOfSize(fontSize);
    textField.placeholder = placeholder;
    textField.text = text;
    [textField setShouldReturnBlock:^BOOL(UITextField *textField) {
        [textField resignFirstResponder];
        return YES;
    }];
    return textField;
    
}

+(instancetype )setupTextField_placeholder:(NSString *)placeholder left:(NSString *)left toTextFieldHeight:(CGFloat)textFieldHeight subView:(UIView *)mainView{
    
    UITextField *phoneTextField = [UITextField new];
    phoneTextField.textColor = TPTitleWhiteColor;
    phoneTextField.backgroundColor = [UIColor clearColor];
    phoneTextField.placeholder = placeholder;
    [phoneTextField setValue:[UIColor grayColor] forKeyPath:@"placeholderLabel.textColor"];
    phoneTextField.keyboardType = UIKeyboardTypeNumberPad;
    
    UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 104.0f, textFieldHeight)];
    leftView.backgroundColor = [UIColor clearColor];
    phoneTextField.leftView = leftView;
    
    UILabel *phoneLeftLabel = [UILabel initWithFrame:CGRectZero font:fontSystemOfSize(16.0) color:TPTitleWhiteColor];
    phoneLeftLabel.textAlignment = NSTextAlignmentRight;
    phoneLeftLabel.adjustsFontSizeToFitWidth = YES;
    phoneLeftLabel.text = left;
    phoneLeftLabel.tag = 110;
    [leftView addSubview:phoneLeftLabel];
    
    
    [phoneLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.top.mas_equalTo(0);
        make.left.mas_equalTo(18.0f);
        make.height.mas_equalTo(leftView.height);
    }];
    
    phoneTextField.borderStyle = UITextBorderStyleNone;
    phoneTextField.font = fontSystemOfSize(16.0f);
    phoneTextField.leftViewMode = UITextFieldViewModeAlways;
    phoneTextField.clearButtonMode =  UITextFieldViewModeWhileEditing;
    [mainView addSubview:phoneTextField];
    
    UIImageView *line = [[UIImageView alloc]initWithImage:[UIImage imageWithColor:TPTitleWhiteColor]];
    line.tag = 120;
    [phoneTextField addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0.0f);
        make.height.mas_equalTo(0.5);
    }];
    
    [phoneTextField setShouldReturnBlock:^BOOL(UITextField *textField) {
        [textField resignFirstResponder];
        return YES;
    }];
    return phoneTextField;
}

-(void)setPlaceholderLabelTextColor:(UIColor *)textColor{
    [self setValue:textColor forKeyPath:@"placeholderLabel.textColor"];
}

-(UIImageView *)xw_addLeftImageViewFrame:(CGRect)frame image:(UIImage *)image imageFrame:(CGRect)imageFrame{
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:imageFrame];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = image;
    [[self xw_addLeftViewFrame:frame] addSubview:imageView];
    return imageView;
}

-(UIButton *)xw_addRightCodeViewFrame:(CGRect)frame string:(NSString *)string btnFrame:(CGRect)btnFrame color:(UIColor *)color{
    
    UIButton *button = [UIButton buttonWithFrame:btnFrame title:string font:fontSystemOfSize(13) color:color];
    ViewBorderRadius(button, btnFrame.size.height/2, 1.0, color);
    [[self xw_addRightViewFrame:frame] addSubview:button];
    return button;
}
-(UIView *)xw_addLeftViewFrame:(CGRect)frame {
    UIView *leftView = [[UIView alloc]initWithFrame:frame];
    leftView.backgroundColor = [UIColor clearColor];
    self.leftViewMode = UITextFieldViewModeAlways;
    self.leftView = leftView;
    return leftView;
}
-(UIView *)xw_addRightViewFrame:(CGRect)frame {
    UIView *rightView = [[UIView alloc]initWithFrame:frame];
    rightView.backgroundColor = [UIColor clearColor];
    self.rightViewMode = UITextFieldViewModeAlways;
    self.rightView = rightView;
    return rightView;
}


@end
